package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Letter;
import com.leoman.entity.LetterAccept;
import com.leoman.service.LetterAcceptService;
import com.leoman.service.LetterAnnexSampleService;
import com.leoman.service.LetterService;
import com.leoman.service.SendPriceService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 信件管理
 * Created by 涂奕恒 on 2016/12/6.
 */
@Controller
@RequestMapping(value = "backend/letter")
public class LetterController extends CommonController {

    @Autowired
    private LetterService letterService;

    @Autowired
    private LetterAcceptService letterAcceptService;

    @Autowired
    private LetterAnnexSampleService letterAnnexSampleService;

    @Autowired
    private SendPriceService sendPriceService;

    @RequestMapping("index")
    public String index(ModelMap model) {
        // 查询寄信价格
        model.addAttribute("money", sendPriceService.getById(1).getMoney());
        model.addAttribute("money1", sendPriceService.getById(2).getMoney());

        return "backend/信件列表";
    }

    @RequestMapping("/accept/index")
    public String acceptIndex(ModelMap model, Integer letterId) {
        model.addAttribute("letterId", letterId);
        return "backend/收信人列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String userName,
                     String startTime,
                     String endTime,
                     Integer type,
                     Integer status,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Letter> page = letterService.pageByParams(userName, startTime, endTime, type, status, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/accept/list")
    public void acceptList(HttpServletResponse response,
                           Integer letterId,
                           Integer draw,
                           Integer start,
                           Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<LetterAccept> page = letterAcceptService.pageByParams(letterId, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer letterId) {
        if (null != letterId) {
            Letter letter = letterService.getById(letterId);
            model.addAttribute("letter", letter);

            // 查询使用物品信息
            model.addAttribute("stamp", letterAnnexSampleService.findOneByLetterAnnexId(letter.getStampId()));
            model.addAttribute("paper", letterAnnexSampleService.findOneByLetterAnnexId(letter.getPaperId()));
            model.addAttribute("mailer", letterAnnexSampleService.findOneByLetterAnnexId(letter.getMailerId()));

            // 如果是发送给熟人的，查询对应的收信信息
            if (letter.getType() == 3 && letter.getMoneyStatus().equals(Constant.GIVE_MONEY_STATUS_YES)) {
                model.addAttribute("letterAcceptInfo", letterAcceptService.findOneByLetterId(letterId));
            }

            model.addAttribute("createTime", DateUtils.longToStringWithFormat(letter.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));

        }

        return "backend/信件详情";
    }

    /**
     * 置顶信件
     *
     * @param letterId 信件ID
     */
    @RequestMapping("/toTop")
    @ResponseBody
    public Integer toTop(Integer letterId) {
        try {
            return letterService.toTop(letterId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除信件
     *
     * @param letterId 信件ID
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer letterId) {
        try {
            letterService.deleteById(letterId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除信件
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            return letterService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改物流信息
     */
    @RequestMapping("/accept/save")
    @ResponseBody
    public Integer acceptSave(Integer letterAcceptId, String expressName, String expressNum) {
        try {
            return letterAcceptService.modifyExpressInfo(letterAcceptId, expressName, expressNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 标记为已打印
     */
    @RequestMapping("/modifyPrintStatus")
    @ResponseBody
    public Integer modifyPrintStatus(Integer id, Integer type) {
        try {
            if (type == 1) {
                return letterService.modifyPrintStatus(id);
            } else {
                return letterAcceptService.modifyPrintStatus(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除收信人信息
     */
    @RequestMapping("/accept/batchDelete")
    @ResponseBody
    public Integer batchDeleteAccept(String ids) {
        try {
            return letterAcceptService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/accept/info")
    public String acceptInfo(ModelMap model, Integer letterAcceptId) {
        if (null != letterAcceptId) {
            LetterAccept letterAccept = letterAcceptService.getById(letterAcceptId);
            model.addAttribute("letterAccept", letterAccept);

            // 查询使用物品信息
            model.addAttribute("stamp", letterAnnexSampleService.findOneByLetterAnnexId(letterAccept.getStampId()));
            model.addAttribute("paper", letterAnnexSampleService.findOneByLetterAnnexId(letterAccept.getPaperId()));
            model.addAttribute("mailer", letterAnnexSampleService.findOneByLetterAnnexId(letterAccept.getMailerId()));

            model.addAttribute("createTime", DateUtils.longToStringWithFormat(letterAccept.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
        }

        return "backend/收信人详情";
    }

    /**
     * 修改寄信价格信息
     */
    @RequestMapping("/updatePrice")
    @ResponseBody
    public Integer updatePrice(Double money, Double money2) {
        try {
            return sendPriceService.updatePrice(money, money2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
